package cn.vc.shop.service;

import cn.vc.shop.entity.po.ShopAct;
import cn.vc.shop.mapper.ShopActMapper;
import com.github.benmanes.caffeine.cache.Cache;
import org.springframework.stereotype.Service;

@Service
public class ShopActService {
    private final ShopActMapper shopActMapper;
    private final Cache<String, ShopAct> shopActCache;
    private final ShopService shopService;

    public ShopActService(ShopActMapper shopActMapper, Cache<String, ShopAct> shopActCache, ShopService shopService) {
        this.shopActMapper = shopActMapper;
        this.shopActCache = shopActCache;
        this.shopService = shopService;
    }



    public int follow(Long userId, Long shopId) {
        ShopAct shopAct = shopActCache.get(
                userId + ":" + shopId,
                key -> {
                    return shopActMapper.selectByUserIdAndShopId(userId, shopId);
                });
        if (shopAct==null) {
            shopAct = new ShopAct();
            shopAct.setUserId(userId);
            shopAct.setShopId(shopId);
            shopAct.setIsFollow(true);
            shopService.follow(shopId);
            return shopActMapper.insert(shopAct)==1?1:0;
        }
        // 以及关注
        else if (shopAct.getIsFollow()) {
            return -1;
        }
        else {
            shopAct.setIsFollow(true);
            shopService.follow(shopId);
            return shopActMapper.updateById(shopAct)==1?1:0;
        }
    }


    /**
     * <h2>评分</h2>
     * @param userId
     * @param shopId
     * @param mark
     * @return
     */
    public boolean marking(Long userId, Long shopId, Integer mark) {
        ShopAct shopAct = shopActCache.get(
                userId + ":" + shopId,
                key -> {
                    return shopActMapper.selectByUserIdAndShopId(userId, shopId);
                });
        if (shopAct==null) {
            shopAct = new ShopAct();
            shopAct.setUserId(userId);
            shopAct.setShopId(shopId);
            shopAct.setMark(mark);
            shopService.marking(shopId, mark);//添加新评分到平均评分
            return shopActMapper.insert(shopAct)==1;
        }
        else {
            //为null，说明第一次评分
            if (shopAct.getMark()==null) {
                shopService.marking(shopId, mark);//添加评分
            }
            //修改评分
            else {
                shopService.noMarking(shopId, shopAct.getMark());//删除旧评分
                shopService.marking(shopId, mark);//添加新评分
            }
            shopAct.setMark(mark);
            return shopActMapper.updateById(shopAct)==1;
        }
    }



    public int noFollow(Long userId, Long shopId) {
        ShopAct shopAct = shopActCache.get(
                userId + ":" + shopId,
                key -> {
                    return shopActMapper.selectByUserIdAndShopId(userId, shopId);
                });
        if (shopAct==null) {
            return 1;
        } else if (!shopAct.getIsFollow()) {
            return -1;
        } else {
            shopAct.setIsFollow(false);
            shopService.noFollow(shopId);
            return shopActMapper.updateById(shopAct)==1?1:0;
        }

    }
}
